package week3;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author The Setting sun
 * @create 2021-09-23 16:32
 */
public class work4 {
    public static void main(String[] args) {
        String[][] datas = {{"aaa", "90"}};
        String[] titles = {"姓名", "成绩"};

        JFrame jf = new JFrame("表格实例");

        JPanel jp1 = new JPanel(new FlowLayout());
        JLabel label1 = new JLabel("姓名");
        label1.setFont(new Font("楷体", Font.BOLD, 18));
        JTextField jtf1 = new JTextField(15);
        jp1.add(label1);
        jp1.add(jtf1);

        JLabel label2 = new JLabel("成绩");
        label2.setFont(new Font("楷体", Font.BOLD, 18));
        JTextField jtf2 = new JTextField(15);
        jp1.add(label2);
        jp1.add(jtf2);

        JButton button = new JButton("添加");

        DefaultTableModel model = new DefaultTableModel(datas, titles) {
            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                // TODO Auto-generated method stub
                return false;
            }
        };

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                model.addRow(new String[]{jtf1.getText(), jtf2.getText()});
            }
        });
        jp1.add(button);


        JPanel jp2 = new JPanel();

        JTable table = new JTable(model);
        jp2.add(new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));

        jf.setLayout(new BorderLayout());
        jf.add(jp1, BorderLayout.NORTH);
        jf.add(jp2, BorderLayout.CENTER);

        jf.setSize(600, 500);
        jf.setVisible(true);
        jf.setLocationRelativeTo(null);
        //frame.pack();
        //设置窗体大小不可变
        jf.setResizable(false);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
